package com.wanxi.dao.Impl;

import com.wanxi.dao.CompanyDao;
import com.wanxi.model.CompanyModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Li
 * @Date 2023/5/6 15:05
 */
public class CompanyDaoImpl implements CompanyDao {
    public int add(CompanyModel companyModel) {
        String sql = "INSERT INTO `companyinfo` ( `logo`, `name`, `land_line`, `mobile_phone`," +
                " `email`, `fax`, `zip_code`, `address`, `create_time`, `update_time`, `enable`, `remark`)" +
                " VALUES ( '" + companyModel.getLogo() + "', '" + companyModel.getName() + "', '" + companyModel.getLandLine() + "'," +
                " '" + companyModel.getMobilePhone() + "', '" + companyModel.getEmail() + "', '" + companyModel.getFax() + "', " +
                "'" + companyModel.getZipCode() + "', '" + companyModel.getAddress() + "', now(), sysdate(), " +
                "'" + companyModel.getEnable() + "', '" + companyModel.getRemark() + "');";
        return JDBC.update(sql);
    }

    /**
     * 查询公司表里是否已存在
     *
     * @param companyModel
     * @return
     */
    public boolean findSameName(CompanyModel companyModel) {
        String sql = "select * from companyinfo where name = '" + companyModel.getName() + "' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    /**
     * 修改公司信息
     *
     * @param companyModel
     * @return
     */
    public int update(CompanyModel companyModel) {
        String sql = "UPDATE `companyinfo` SET `logo` = '" + companyModel.getLogo() + "', `name` = '" + companyModel.getName() + "', " +
                "`land_line` = '" + companyModel.getLandLine() + "', `mobile_phone` = '" + companyModel.getMobilePhone() + "', " +
                "`email` = '" + companyModel.getEmail() + "', `fax` = '" + companyModel.getFax() + "', `zip_code` = '" + companyModel.getZipCode() + "'," +
                " `address` = '" + companyModel.getAddress() + "', `update_time` = now(), " +
                "`enable` = '" + companyModel.getEnable() + "', `remark` = '" + companyModel.getRemark() + "' WHERE (`id` = '" + companyModel.getId() + "');";
        return JDBC.update(sql);
    }

    /**
     * 删除公司信息
     *
     * @param companyModel
     * @return
     */
    public int delete(CompanyModel companyModel) {
        String sql = "delete from companyinfo where id = '" + companyModel.getId() + "' ";
        return JDBC.update(sql);
    }


    public List<CompanyModel> findAll(CompanyModel companyModel) {
        int start = (companyModel.getPage()-1)*companyModel.getLimit();
        String sql = "select * from companyinfo where true";
         sql += getWhereSql(companyModel);
        sql += " order by id desc";
        sql += " limit "+start+","+companyModel.getLimit()+" ";
        ResultSet query = JDBC.query(sql);
        List<CompanyModel> list = new ArrayList<CompanyModel>();
        try {
            while (query.next()) {
                list.add(CompanyModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .logo(query.getString("logo"))
                        .landLine(query.getString("land_line"))
                        .mobilePhone(query.getString("mobile_phone"))
                        .email(query.getString("email"))
                        .fax(query.getString("fax"))
                        .zipCode(query.getString("zip_code"))
                        .address(query.getString("address"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .build());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    private String  getWhereSql(CompanyModel companyModel){
        String sql = "";
        if (!"".equals(companyModel.getName())) {
            sql += " and name like '%" + companyModel.getName() + "%' ";
        }
        if (!"".equals(companyModel.getAddress())) {
            sql += " and address like '%" + companyModel.getAddress() + "%' ";
        }
        return sql;
    }
    public CompanyModel findById(CompanyModel companyModel) {
        String sql = " select * from companyinfo where id = '" + companyModel.getId() + "' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
            return CompanyModel.builder()
                    .id(query.getInt("id"))
                    .name(query.getString("name"))
                    .logo(query.getString("logo"))
                    .mobilePhone(query.getString("mobile_phone"))
                    .address(query.getString("address"))
                    .landLine(query.getString("land_line"))
                    .zipCode(query.getString("zip_code"))
                    .fax(query.getString("fax"))
                    .email(query.getString("email"))
                    .enable(query.getInt("enable"))
                    .remark(query.getString("remark"))
                    .build();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer count(CompanyModel companyModel) {
        String sql = "select count(*) as count from companyinfo where true";
      sql += getWhereSql(companyModel);
        return JDBC.count(sql);
    }

    @Override
    public Integer totalCount() {
        return null;
    }
}
